import { Injectable, OnModuleInit } from '@nestjs/common';
import axios from 'axios';

@Injectable()
export class UserService implements OnModuleInit {
  // export class UserService{
  onModuleInit() {
    console.log('模块初始化');
  }
  userInfo = [
    {
      id: 0,
      name: '张三',
    },
    {
      id: 1,
      name: '李四',
    },
    {
      id: 2,
      name: '李四',
    },
    {
      id: 3,
      name: '王五',
    },
    {
      id: 4,
      name: '赵六',
    },
    {
      id: 5,
      name: '王八',
    },
  ];

  async userList(): Promise<any[]> {
    return this.userInfo;
  }
  // let userInfo=[{name:'zhangsan'}]
  // 默认获取所有用户信息 get接口
  async index(): Promise<any[]> {
    return this.userInfo;
  }
  // 新增用户信息 post接口
  async addUser(body): Promise<any> {
    // console.log(body);
    return `${JSON.stringify(body)},用户传递的数据是`;
  }
  // 获取单个用户信息 get接口
  async getSingleUserInfo(id: number): Promise<any> {
    console.log(id);
    let index = this.userInfo.findIndex(i => i.id === id);
    if (index === -1) {
      return '没有该用户';
    } else {
      return this.userInfo[index];
    }
  }
  // 删除用户信息 delete接口
  async deleteUser(id: number): Promise<any> {
    let index = this.userInfo.findIndex(i => i.id === id);
    if (index === -1) {
      return `该用户不存在`;
    } else {
      return this.userInfo[index];
    }
  }
  // 编辑用户信息
  async editUserInfo(body) {
    // console.log(body);
    let id = Number(body.id);
    if (this.userInfo[id]) {
      return this.userInfo[id];
    } else {
      return `该用户信息不存在`;
    }
  }
}
